package com.engine.ch.cron;

import com.alibaba.fastjson.JSONObject;
import com.engine.ch.action.ModeExpandTemplate;
import com.engine.ch.action.WorkflowToPdfAction;
import com.engine.ch.entity.Archive;
import com.engine.ch.service.impl.SyncArchiveImpl;
import com.engine.common.util.ServiceUtil;
import com.engine.workflow.biz.requestForm.WfToDocBiz;
import com.engine.workflow.service.HtmlToPdfService;
import com.engine.workflow.service.impl.HtmlToPdfServiceImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import java.io.File;
import java.util.*;
import java.util.logging.Logger;

import static com.api.ecme.excel.HtmlLayoutOperate.user;

/**
 * 计划任务调用 档案同步
 *
 * @author: cloudyue-jason
 * @date  : 2025/11/17 13:51
 * @corp  : Copyright© 2023-Future CloudYue
 */
public class SyncArchiveCron extends BaseCronJob {
    private static final Log log = LogFactory.getLog(SyncArchiveCron.class);

    @Override
    public void execute() {
        try {
            SyncArchiveImpl syncArchive = new SyncArchiveImpl();
            RecordSet recordSet = new RecordSet();
            String sql = "select * from uf_gwygdtsdaxtb where  sflssj = 1 and (tsjg is NULL or tsjg =1)";
            recordSet.executeQuery(sql);

            List<Archive> list = new ArrayList<Archive>();
                String workId = "";
                String nodeId = "";
                String userid = "";
                while(recordSet.next()){
                String lcrequestid = recordSet.getString("lcrequestid");
                String wjid = recordSet.getString("wjid");
                String bt = recordSet.getString("bt");
                String szbm = recordSet.getString("szbm");
                String wjbhfwzh = recordSet.getString("wjbhfwzh");
                String zrzgs = recordSet.getString("zrzgs");
                String zrzbm = recordSet.getString("zrzbm");
                String cwrq = recordSet.getString("cwrq");
                int ys = recordSet.getInt("ys");
                String mj = recordSet.getString("mj");
                String ndhfwzh = recordSet.getString("ndhfwzh");
                String bgqx = recordSet.getString("bgqx");
                String fj = recordSet.getString("fj");
                String zw = recordSet.getString("zw");
                Map<String, Object> params = new HashMap<>();
                params.put("useWk","1"); //是否使用wkhtmltopdf插件转pdf 1：是  0：否  不传则默认使用Itext插件
                params.put("requestid",lcrequestid);  //必传
                String sql1 = "SELECT WORKFLOWID,CURRENTNODEID from workflow_requestbase WHERE REQUESTID = '"+lcrequestid+"' AND CURRENTNODETYPE =3";
                RecordSet rs = new RecordSet();
                rs.executeSql(sql1);
                if(rs.next()){
                    workId = Util.null2String(rs.getString("WORKFLOWID"));
                    nodeId = Util.null2String(rs.getString("CURRENTNODEID"));
                }
                String sql2 = "SELECT id from workflow_nodehtmllayout where WORKFLOWID = "+workId+" and NODEID = "+nodeId;
                RecordSet rs1 = new RecordSet();
                rs1.executeSql(sql2);
                if(rs1.next()){
                    params.put("modeid",Util.null2String(rs1.getString("id")));  //模板id(传模板id则根据模板生成.不传则默认使用显示模板)
                }
                String sql3 = "SELECT CREATER from workflow_requestbase WHERE REQUESTID = '"+lcrequestid+"'";
                RecordSet rs2 = new RecordSet();
                rs2.executeSql(sql3);
                if(rs2.next()){
                    userid = rs2.getString("CREATER");
                }
                String sql4 = "select * from hrmresource where id = '"+userid+"'";
                RecordSet rs3 = new RecordSet();
                rs3.executeSql(sql4);
                User user = new User();
                if(rs3.next()){
                    userid = Util.null2String(rs3.getString("id"));  //用户id(不传则默认使用当前用户)
                    user.setUid(Integer.parseInt(userid));
                    user.setLastname(rs3.getString("lastname"));
                    user.setLoginid(rs3.getString("loginid"));
                    user.setAccount(rs3.getString("account"));
                    user.setEmail(rs3.getString("email"));
                    user.setMobile(rs3.getString("mobile"));
                }
                params.put("path","/data/zhpdf"); //存储路径(不传则windows默认D:/testpdf;linux默认/usr/testpdf)
                params.put("onlyHtml","0"); //0:转pdf  1:只转成html  2:转html和pdf  (不传则默认=0)
                params.put("keepsign","1");  //1:保留底部签字意见 0：不保留 (不传则默认=1)
                params.put("pageSize","100"); //底部签字意见最大显示数量  (默认=100)
                params.put("isTest", "1"); //外部调用必传isTest=1
                params.put("limitauth", "0"); //不校验权限
                HtmlToPdfService htmlToPdfService = (HtmlToPdfService) ServiceUtil.getService(HtmlToPdfServiceImpl.class,user);
                Map<String,Object> pathMap = htmlToPdfService.getFormDatas(params);
                Object path = pathMap.get("path");
                Object filename = pathMap.get("filename");
                WorkflowToPdfAction workflowToDoc = new WorkflowToPdfAction();
                // 保存ImageFile
                File file = new File(path.toString());
                long fileSize = file.length();
                int i = workflowToDoc.saveImageFile(filename.toString(), path.toString(), fileSize, true);

                // 保存docdetail
                WfToDocBiz wfToDocBiz = new WfToDocBiz();
                RequestManager requestManager =  new RequestManager();
                LinkedHashMap<String, String> map = new LinkedHashMap<>();
                map.put("offline_pdf",String.valueOf(i));
                workflowToDoc.saveDocDetail(filename.toString(),workId,lcrequestid,"",user.getLoginid(),map,requestManager,wfToDocBiz);
                Integer fileId = workflowToDoc.getFileId();
                // 保存DocImageFile
//                workflowToDoc.saveDocImageFile(fileId,i,filename.toString(),3);
                String spjlsfwdpdf = String.valueOf(fileId);
                String fwrq = recordSet.getString("fwrq");
                String swsj = recordSet.getString("swsj");
                String sfwlx = recordSet.getString("sfwlx");
                String zsdw = recordSet.getString("zsdw");
                Integer tsjg = recordSet.getInt("tsjg");
                String tsjgsm = recordSet.getString("tsjgsm");
                String tssj = recordSet.getString("tssj");
                Integer sflssj = recordSet.getInt("sflssj");
                String lwdw = recordSet.getString("lwdw");


                Archive archive = new Archive(lcrequestid, wjid, bt, szbm, wjbhfwzh, zrzgs, zrzbm, cwrq, ys, mj, ndhfwzh, bgqx, fj, zw, spjlsfwdpdf, fwrq, swsj, sfwlx, zsdw, tsjg, tsjgsm, tssj, sflssj,lwdw);
                list.add(archive);
            }
            for (Archive archive : list) {
                JSONObject syncObj = syncArchive.sync(archive);
            }
        } catch (Exception e) {
            log.error("档案同步异常:" + e.getMessage());

        }
    }
}
